/*
 * This file is a part of the SWFer project.
 *
 * Copyright (C) 2012 Michael Bradshaw <mjbshaw@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "Swf.hpp"
#include "Tags.hpp"
#include "ByteReader.hpp"

#include <fstream>
#include <iostream>

namespace swfer
{

Swf::Swf()
{

}

Swf::Swf(const std::string& path)
{
    open(path);
}

void Swf::open(const std::string& path)
{
	packets.clear();

    std::ifstream file(path.c_str(), std::ios::binary);

    ByteReader reader(file);

    reader.read(header);
    std::cout << "signature: ";
    std::cout.write((const char*)header.signature, 3);
    std::cout << std::endl;
    std::cout << "version: " << (int)header.version << std::endl;
    std::cout << "file length: " <<  header.fileLength << std::endl;
    std::cout << "frame bits: " << header.frameSize.bits << std::endl;
    std::cout << "frame xmin: " << header.frameSize.xMin << std::endl;
    std::cout << "frame xmax: " << header.frameSize.xMax << std::endl;
    std::cout << "frame ymin: " << header.frameSize.yMin << std::endl;
    std::cout << "frame ymax: " << header.frameSize.yMax << std::endl;
    std::cout << "fps: " << (int)header.frameRate.integer << '.' << (int)header.frameRate.fraction << std::endl;
    std::cout << "frame count: " << header.frameCount << std::endl;

    Packet packet;
	do
	{
		reader.read(packet);
		packets.push_back(packet);
		std::cout << tagCodeToString(packet.header.code) << std::endl;
	} while (packet.header.code != END);
}

const std::list<Packet>& Swf::getPackets() const
{
	return packets;
}

}